package com.by.and_widget.banner;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;

import com.by.and_widget.CircleIndicator;

/**
 * Banner
 */
public class BannerLayout extends FrameLayout implements View.OnTouchListener, GestureDetector.OnGestureListener {
    private static final int DISTANCE = 25;

    private AutoScrollerViewFilpper mViewFilpper;
    private CircleIndicator mIndicator;
    private GestureDetector mGestureDetector;

    public BannerLayout(Context context) {
        super(context);
        initView();
    }

    public BannerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public BannerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        mGestureDetector = new GestureDetector(getContext(), this);
        mViewFilpper = new AutoScrollerViewFilpper(getContext());
        mViewFilpper.setOnTouchListener(this);
        mViewFilpper.setOnPageSelectListener(new AutoScrollerViewFilpper.OnPageSelectListener() {
            @Override
            public void onPageSelect(int pos) {
                if (mIndicator != null) {
                    mIndicator.chooseItem(pos);
                }
            }
        });
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(mViewFilpper, lp);

        mIndicator = new CircleIndicator(getContext());
        FrameLayout.LayoutParams mIndicatorLp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mIndicatorLp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        mIndicatorLp.bottomMargin = dpTpPx(2);
        addView(mIndicator, mIndicatorLp);
    }

    public void setAdapter(BaseAdapter mAdapter) {
        if (mAdapter != null && mAdapter.getCount() > 0) {
            mViewFilpper.setAdapter(mAdapter);
            mIndicator.setCount(mAdapter.getCount(), 0);
        }
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener l) {
        if (mViewFilpper != null) {
            mViewFilpper.setOnItemClickListener(l);
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return mGestureDetector.onTouchEvent(motionEvent);
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        mViewFilpper.stopFlipping();
        return true;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) {
        Log.e("banner", "onFling:");
        if (e1.getX() - e2.getX() > dpTpPx(DISTANCE)) {
//            mViewFilpper.setInAnimation(AnimationUtils.loadAnimation(this,
//                    R.anim.push_left_in));
//            mViewFilpper.setOutAnimation(AnimationUtils.loadAnimation(this,
//                    R.anim.push_left_out));
            mViewFilpper.showNext();// 向右滑动
            return true;
        } else if (e1.getX() - e2.getX() < -1 * dpTpPx(DISTANCE)) {
//            mViewFilpper.setInAnimation(AnimationUtils.loadAnimation(this,
//                    R.anim.push_right_in));
//            mViewFilpper.setOutAnimation(AnimationUtils.loadAnimation(this,
//                    R.anim.push_right_out));
            mViewFilpper.showPrevious();// 向左滑动
            return true;
        }
        return false;
    }

    private int dpTpPx(float value) {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, dm) + 0.5);
    }


}
